package tree;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import tree.BinaryTree.Visitor;
import tree.printer.BinaryTrees;

public class TreeTest {
	@Test
	public void testBinarySearchTree() throws Exception {
		Integer[] arr = new Integer[]{39, 23, 10, 5, 35, 15, 34, 33, 6, 41, 9, 14, 4};
		BST<Integer> bst = new BST<>();
		for (int i = 0; i < arr.length; i++) {
			bst.add(arr[i]);
		}
		BinaryTrees.print(bst);
		System.out.println();
		bst.preorderTraversal(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return element == 34;
			}
		});
		System.out.println();
		bst.preorderTraversalByStack(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return element == 34;
			}
		});
		System.out.println();
		bst.inorderTraversal(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return element == 35;
			}
		});
		System.out.println();
		bst.inorderTraversalByStack(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return element == 35;
			}
		});
		System.out.println();
		bst.postorderTraversal(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return element == 47;
			}
		});
		System.out.println();
		bst.postorderTraversalByStack(new Visitor<Integer>() {
			@Override
			public boolean visit(Integer element) {
				System.out.print(element + " ");
				return element == 47;
			}
		});
	}
	
	@Test
	public void testRemove() throws Exception {
		List<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		
	}
	
	@Test
	public void testAVLTree() throws Exception {
		Integer[] arr = new Integer[]{32, 1, 25, 37, 36, 24, 5, 29, 35, 16, 14, 40, 26, 21, 19, 17};
		BST<Integer> avl = new AVLTree<>();
		for (int i = 0; i < arr.length; i++) {
			avl.add(arr[i]);
		}
		BinaryTrees.print(avl);
		System.out.println();
	}
	
	@Test
	public void testRBTree() throws Exception {
		Integer[] arr = new Integer[]{32, 1, 25, 37, 36, 24, 5, 29, 35, 16, 14, 40, 26, 21, 19, 17};
		BST<Integer> rb = new RBTree<>();
		for (int i = 0; i < arr.length; i++) {
			rb.add(arr[i]);
		}
		BinaryTrees.print(rb);
		System.out.println();
		System.out.println("============================================");
		for (int i = 0; i < arr.length; i++) {
			rb.remove(arr[i]);
			System.out.println("=====================["+arr[i]+"]=======================");
			BinaryTrees.print(rb);
			System.out.println();
		}
	}
}
